args <- commandArgs(TRUE)
snp<-read.table(args[4],sep="\t",head=T)
gene<-read.table("/data1/bsi/BORA_processing/devel/eqtl/parallelize_genotypingsurvival/Matrix_eQTL_R_Source/GE.txt",sep="\t",head=T)
k1<-c("gene","snp","Intercept_Estimate","snp_Estimate","Intercept_Std.Error","snp_Std.Error","Intercept_t value","snp_t value","Intercept_Pr(>|t|)","snp_Pr(>|t|)")
data<-read.table(args[1],sep="\t",head=T)
colnames(data)<-c("rsid","geneid")
#sorting accordind to file order
sort_order<-read.table(args[3],head=T)
sort_order<-as.vector(t(sort_order))
gene<-gene[,sort_order]
rownames(gene)<-gene$geneid

data<-merge(data,snp,by="rsid")
data<-merge(data,gene,by="geneid")
 
zz <- file(args[2],"w")
writeLines(paste(k1,collapse=" "),con=zz,sep="\n")
i<-1
while(i<nrow(data))
{
        print(i)
	g2<-as.numeric(data[i,3:209])
        g1<-as.numeric(data[i,210:416])
	kk<-c()
	kk<-c(as.character(data[i,2]),as.character(data[i,1]),as.vector(coefficients(summary(lm(g1 ~ g2, family=gaussian(link="log"))))))
	writeLines(paste(kk,collapse=" "),con=zz,sep="\n")
	i<-i+1
}
close(zz)
